package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.ZjSysProjectNumberBo;
import com.ruoyi.zhgd.domain.vo.ZjSysProjectNumberVo;
import com.ruoyi.zhgd.domain.ZjSysProjectNumber;
import com.ruoyi.zhgd.mapper.ZjSysProjectNumberMapper;
import com.ruoyi.zhgd.service.IZjSysProjectNumberService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 项目标段编号信息Service业务层处理
 *
 * @author tll
 * @date 2024-03-13
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class ZjSysProjectNumberServiceImpl implements IZjSysProjectNumberService {

    private final ZjSysProjectNumberMapper baseMapper;

    /**
     * 查询项目标段编号信息
     */
    @Override
    public ZjSysProjectNumberVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询项目标段编号信息列表
     */
    @Override
    public TableDataInfo<ZjSysProjectNumberVo> queryPageList(ZjSysProjectNumberBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZjSysProjectNumber> lqw = buildQueryWrapper(bo);
        Page<ZjSysProjectNumberVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询项目标段编号信息列表
     */
    @Override
    public List<ZjSysProjectNumberVo> queryList(ZjSysProjectNumberBo bo) {
        LambdaQueryWrapper<ZjSysProjectNumber> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZjSysProjectNumber> buildQueryWrapper(ZjSysProjectNumberBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZjSysProjectNumber> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, ZjSysProjectNumber::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectNumber()), ZjSysProjectNumber::getProjectNumber, bo.getProjectNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getSectionNumber()), ZjSysProjectNumber::getSectionNumber, bo.getSectionNumber());
        lqw.like(StringUtils.isNotBlank(bo.getProjectName()), ZjSysProjectNumber::getProjectName, bo.getProjectName());
        lqw.like(StringUtils.isNotBlank(bo.getSectionName()), ZjSysProjectNumber::getSectionName, bo.getSectionName());
        return lqw;
    }

    /**
     * 新增项目标段编号信息
     */
    @Override
    public Boolean insertByBo(ZjSysProjectNumberBo bo) {
        ZjSysProjectNumber add = BeanUtil.toBean(bo, ZjSysProjectNumber.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改项目标段编号信息
     */
    @Override
    public Boolean updateByBo(ZjSysProjectNumberBo bo) {
        ZjSysProjectNumber update = BeanUtil.toBean(bo, ZjSysProjectNumber.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZjSysProjectNumber entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除项目标段编号信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
